package com.ee.chat.api.controller;

import com.ee.chat.api.service.ILoginService;
import com.ee.chat.common.bean.Result;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("login")
public class LoginController {
    private Logger log = LoggerFactory.getLogger(LoginController.class);

    @Resource
    ILoginService loginService;

    /**
     * 注册登录
     * @param type      登录方式，0-手机号  1-qq  2-微信
     * @param thirdId   登录标识，手机号或者unionid
     * @param name      用户昵称(注册时必填)
     * @param icon      用户头像(注册时必填)
     * @param code      手机验证码(手机号登录注册时必填)
     * @param inviteCode    邀请码
     * @param channelCode   渠道编码
     * @return
     */
    @RequestMapping("login")
    public Result login(HttpServletRequest request, @RequestParam Integer type, @RequestParam String thirdId, @RequestParam(required = false) String name,
                        @RequestParam(required = false) String icon, @RequestParam(required = false) String code,
                        @RequestParam(required = false) String inviteCode, @RequestParam(required = false) String channelCode,
                        @RequestParam(required = false) String channelCode2,
                        @RequestParam String extra){
        try {
            return loginService.login(request, type, thirdId, name, icon, code, inviteCode, channelCode, channelCode2, extra);
        } catch (Exception e){
            log.error("【登录错误】", e);
        }
        return Result.returnFailure();
    }

    /**
     * 登出
     * @Author Shane
     * @Date 2019/7/25 0:20
     * @param
     * @return
     */
    @RequestMapping("logout")
    public Result logout(@RequestParam String token){
        try {
            return loginService.logout(token);
        } catch (Exception e){
            log.error("【登出错误】", e);
        }
        return Result.returnFailure();
    }

//    @RequestMapping("batchRegisterIM")
    public Result batchRegisterIM(){
        try {
            return loginService.batchRegisterIM();
        } catch (Exception e){
            log.error("【错误】", e);
        }
        return Result.returnFailure();
    }

    /**
     * 获取授权信息
     * @Author Shane
     * @Date 2020/5/30 0:27
     * @param type  授权类型，2-微信
     * @param code  type=2时必传
     * @return
     */
    @RequestMapping("getAuthInfo")
    public Result getAuthInfo(@RequestParam Integer type, String code){
        try {
            return loginService.getAuthInfo(type, code);
        } catch(Exception e){
            log.error("【获取授权信息错误】", e);
        }
        return Result.returnFailure();
    }

    /**
     * 更新用户基本信息
     * @Author Shane
     * @Date 2020/7/10 12:58
     * @param icon 头像
     * @param name 昵称
     * @param gender    性别，1-男，2-女
     * @return 
     */
    @RequestMapping("updateUserInfo")
    public Result updateUserInfo(@RequestParam String token, @RequestParam String icon, @RequestParam String name, @Param("gender") Integer gender){
        try {
            return loginService.updateUserInfo(token, icon, name, gender);
        } catch(Exception e){
            log.error("【更新用户基本信息错误】", e);
        }
        return Result.returnFailure();
    }
}
